
export default function typeFilter(ftype,cname,comment) {      
    let msg = { state: false, type: ftype };
    let fmsg = {state:false};
    fmsg = dataFilter(ftype,cname);
    if (fmsg.state) {
        return fmsg;
    }
    fmsg = upLoadFilter(ftype,cname);
    if (fmsg.state) {
        return fmsg;
    }
    fmsg = stateFilter(ftype,cname,comment);
    if (fmsg.state) {
        fmsg.mapsStr = "[";
        let items = [];
        fmsg.maps.forEach(element => {
            items.push(jsonToStr(element));
        });
        fmsg.mapsStr +=items.join(", ");
        fmsg.mapsStr +="]";
        
        return fmsg;
    }

    return msg;
}

function jsonToStr(json){
    let clone = "{ ";
    let vs = [];
    for (const key in json) {
        if (json.hasOwnProperty(key)) {
            let element = json[key];   
            if(typeof element =="string"){
                element= "'"+element+"'";
            }
            vs.push(key+": "+element)         
        }
    }
    let vstr = vs.join(", ")
    return clone+vstr+" }";
}

function dataFilter(fType,cname) {
    let dataTest = /time/;
    if ((fType == 'int' && dataTest.test(cname)) || fType == "date") {
        return {state:true,type:"date"};
    } else {
        return {state:false};
    }
}

function upLoadFilter(fType,cname) {
   // console.log(cname)
    let dataTest = /img/;
    //console.log(dataTest.test(cname))
    if ((dataTest.test(cname)) && fType == "varchar") {
        return {state:true,type:"upLoad"};
    } else {
        return {state:false};
    }
}

function stateFilter(fType,cname,comment) {
  //  console.log(fType,cname,comment);
    
    let stateTest1 = /是否/;
    let stateTest2 = /[0-9]为\D+/g;

    let stateTest3 = /[0-9]\.\D+/g;   

    let stateTest4 = /[0-9]\D+/g; 
    
    if(stateTest1.test(comment)){
        let maps = [];
        maps.push({value:1,name:"是"})
        maps.push({value:0,name:"否"})
        return {state:true,type:"boolean",maps:maps};
    }

    if(stateTest2.test(comment)){
        let maps = [];
        let curStr = comment;
        let ms = curStr.match(stateTest2);
        for (let index = 0; index < ms.length; index++) {
            const element = ms[index];
            let vs = element.split("为");  
            maps.push({value:vs[0],name:vs[1]})          
        }       
        return {state:true,type:"switch",maps:maps};
    }

    if(stateTest3.test(comment)){
        let maps = [];
        let curStr = comment;
        let ms = curStr.match(stateTest3);
        for (let index = 0; index < ms.length; index++) {
            const element = ms[index];
            let vs = element.split(".");  
            maps.push({value:vs[0],name:vs[1]})          
        }       
        return {state:true,type:"switch",maps:maps};
    }

    if(stateTest4.test(comment)){
        let maps = [];
        let curStr = comment;
        let ms = curStr.match(stateTest4);       
        
        for (let index = 0; index < ms.length; index++) {
            const element = ms[index];
            let vs = element.split(".");  
            let sss = /，|,/g;
            maps.push({value:vs[0].substring(0,1),name:vs[0].substring(1,vs[0].length).replace(sss,"")})          
        }        
        return {state:true,type:"switch",maps:maps};
    }

    
    return {state:false};
}